from bs4 import BeautifulSoup


def fetch_movie_info():
    # 读取文件
    page1 = open('page1.html', 'rb')

    # 使用BeautifulSoup解析HTML
    text = page1.read()
    soup = BeautifulSoup(text, 'lxml')

    # 找到所有电影列表
    movies = soup.find_all('div', class_='el-card item m-t is-hover-shadow')  # 假设每个电影信息都在一个class为movie-info的div中

    # 存储电影信息的列表
    movie_list = []

    for movie in movies:
        # 遍历所有电影,提取关注信息
        country, duration, rating, release_date, title = movie_detail_parse(movie)

        movie_list.append({
            '电影名称': title,
            '国家': country,
            '时长': duration,
            '上映时间': release_date,
            '剧情类型': rating
        })

    return movie_list


def movie_detail_parse(movie):
    # 提取每个电影的具体信息，这里需要根据实际HTML结构调整
    # 假设每个div中有title, country, duration, release_date, rating等标签
    title = movie.find('h2').get_text(strip=True) if movie.find('h2') else ''
    country = movie.find('div', class_='m-v-sm info').find('span').get_text(strip=True)
    movie_info1 = movie.find('div', class_='m-v-sm info').find_all('span')
    duration = movie_info1[2].string
    movie_info = movie.find_all('div', class_='m-v-sm info')
    try:
        release_date = movie_info[1].find('span').string
    except:
        release_date = "上映时间缺失"
        pass
    buttons = movie.find_all('button')
    rating = [button.find('span').string for button in buttons]
    return country, duration, rating, release_date, title


movies = fetch_movie_info()
for movie in movies:
    print(movie)


# todo 将这个代码改为提取前10页电影信息
